<!-- 搜索框 -->
<div class="single-filter-box">
	<button class="layui-btn" onclick="add()">添加帮助内容</button>

	<div class="layui-form">
		<div class="layui-input-inline">
			<input type="text" name="search_text" placeholder="请输入帮助名称" autocomplete="off" class="layui-input">
			<button type="button" class="layui-btn layui-btn-primary" lay-filter="search" lay-submit>
				<i class="layui-icon">&#xe615;</i>
			</button>
		</div>
	</div>
</div>

<!-- 列表 -->
<table id="help_list" lay-filter="help_list"></table>

<!-- 操作 -->
<script type="text/html" id="operation">
	<div class="table-btn">
		<a class="layui-btn" lay-event="promote">推广</a>
		<a class="layui-btn" lay-event="edit">编辑</a>
		<a class="layui-btn" lay-event="delete">删除</a>
	</div>
</script>
<script type="text/html" id="sort">
	<input name="sort" type="number" onchange="editSort({{d.id}},this)" value="{{d.sort}}" placeholder="请输入排序" class="layui-input edit-sort len-short">
</script>

<!-- 推广 -->
{include file="app/shop/view/promote.html"}

<script>
	var form,laytpl,table, repeat_flag = false;//防重复标识;
	layui.use(['form','laytpl'], function() {
		form = layui.form;
		laytpl = layui.laytpl;
		form.render();

		table = new Table({
			elem: '#help_list',
			url: ns.url("shop/help/helpList"),
			cols: [
				[ {
					field: 'title',
					title: '帮助标题',
					width: '30%',
					unresize: 'false'
				}, {
					field: 'class_name',
					title: '帮助分类',
					width: '20%',
					unresize: 'false'
				},{
                    field: 'sort',
                    title: '排序',
                    width: '15%',
                    sort : true,
                    unresize: 'false',
                    templet: '#sort'
                }, {
					field: 'create_time',
					title: '创建时间',
					width: '20%',
					unresize: 'false',
					templet: function (data) {
						return ns.time_to_date(data.create_time);
					}
				}, {
					title: '操作',
					toolbar: '#operation',
					unresize: 'false',
                    align : 'right'
				}]
			],
		});
		
		/**
		 * 搜索功能
		 */
		form.on('submit(search)', function(data) {
			table.reload({
				page: {
					curr: 1
				},
				where: data.field
			});
		});

        table.on("sort",function (obj) {
            table.reload({
                page: {
                    curr: 1
                },
                where: {
                    order:obj.field,
                    sort:obj.type
                }
            });
        });
		
		/**
		 * 监听工具栏操作
		 */
		table.tool(function(obj) {
			var data = obj.data;
			switch (obj.event) {
				case 'promote': // 推广
					promote(data);
					break;
				case 'edit': //编辑
					location.hash = ns.hash("shop/help/editHelp?id=" + data.id);
					break;
				case 'delete': //删除
					deleteHelp(data.id);
					break;
			}
		});
	});

	/**
	 * 删除
	 */
	function deleteHelp(id) {
		if (repeat_flag) return false;
		repeat_flag = true;

		layer.confirm('确定要删除该帮助内容吗?', function (index) {
				layer.close(index);
				$.ajax({
					url: ns.url("shop/help/deleteHelp"),
					data: {id},
					dataType: 'JSON',
					type: 'POST',
					success: function (res) {
						layer.msg(res.message);
						repeat_flag = false;

						if (res.code == 0) {
							table.reload();
						}
					}
				});
			},
			function () {
				repeat_flag = false;
				layer.close();
			});
	}

	// 监听单元格编辑
	function editSort(id, event) {
		var data = $(event).val();
		if (!new RegExp("^-?[1-9]\\d*$").test(data)) {
			layer.msg("排序号只能是整数");
			return;
		}
		if(data<0){
			layer.msg("排序号必须大于0");
			return ;
		}
		$.ajax({
			type: 'POST',
			url: ns.url("shop/help/modifySort"),
			data: {
				sort: data,
				help_id: id
			},
			dataType: 'JSON',
			success: function(res) {
				layer.msg(res.message);
				if (res.code == 0) {
					listenerHash(); // 刷新页面
				}
			}
		});
	}

	function promote(data) {
		if (repeat_flag) return;
		repeat_flag = true;
		getHelpQrcode(data.id,'h5',function (res) {
			res.id = data.id;
			repeat_flag = false;
			laytpl($("#promote").html()).render(res, function (html) {
				layer.open({
					type: 1,
					area: ['730px', '450px'],
					offset: '155px',
					title: ['推广'],
					content: html,
					success: function(){
						$('input[name="promote_type"][value="h5"]').attr('data-config',JSON.stringify(res.h5));
						form.render();

						// 推广渠道监听
						promoteTypeSwitch();

						// 二次查询微信小程序、支付宝小程序二维码
						getHelpQrcode(data.id,'all',function (res) {
							$('input[name="promote_type"]').each(function (index,item) {
								if(res[$(this).val()]){
									$(this).attr('data-config',JSON.stringify(res[$(this).val()]))
								}else{
									$(this).attr('data-config',JSON.stringify({ type : $(this).val(), path : '' }))
								}
							})

						});
					}
				});
			});
		});
	}

	function getHelpQrcode(help_id,app_type,callback) {
		$.ajax({
			type: "POST",
			url: ns.url("shop/help/promote"),
			data: {
				help_id,
				app_type
			},
			dataType: 'JSON',
			success: function (res) {
				if (callback) callback(res.data);
			}
		});
	}

	function add() {
		location.hash = ns.hash("shop/help/addHelp");
	}
</script>
